package com.oromwin.repos.socket;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import org.apache.commons.logging.Log;

import com.oromwin.repos.common.Logger;
import com.oromwin.repos.common.ReposClientConfig;
import com.oromwin.repos.server.request.file.CreateFileRequest;

public class ReposSocketClient extends SocketTestCase {
    private Log log = Logger.getLog(ReposSocketClient.class);
    private final static int COUNT = 10;

    public ReposSocketClient(String name) {
        super(name);
        log.info("");
    }
    
    private void sendFile() {
        // Create a socket with a timeout
        try {
            InetAddress addr = InetAddress.getByName(ReposClientConfig.getInstance().getReposSocketServerIP());
//            int port = PORT;
            SocketAddress sockaddr = new InetSocketAddress(addr, ReposClientConfig.getInstance().getReposSocketPort());
            // Create an unbound socket
            Socket sock = new Socket();
            // This method will block no more than timeoutMs.
            // If the timeout occurs, SocketTimeoutException is thrown.
            int timeoutMs = 3000;   // 2 seconds
            
            sock.connect(sockaddr, timeoutMs);
            File file = new File(getROOTPATH() + INPUT);
            InputStream is = new FileInputStream(file);
            OutputStream os = sock.getOutputStream();
            DataOutputStream dos = new DataOutputStream(os);
            long length = file.length();
            dos.writeLong(length);
            int chunkSize = 4096;
            byte[] b = new byte[chunkSize];
            int read = 0;
            while ((read = is.read(b, 0, chunkSize)) > 0) {
                dos.write(b, 0, read);
                length -= read;
                if (length <= 0) {
                    break;
                }
            }
            int i = 0;
            InputStream res = sock.getInputStream();
            while ((read = res.read()) > 0) {
//            while ((read = res.read(b, 0, chunkSize)) > 0) {
                b[i++] = (byte)read;
            }
            String result = new String(b, 0, i);
            System.out.println(result);
            is.close();
            dos.close();
            sock.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketTimeoutException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void _testSendFile() {
        long start = System.currentTimeMillis();
        for (int i = 0; i < COUNT; i++) {
            sendFile();
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    public void _testSendFileCreateRequest() {
    	boolean DEBUG = false;
        try {
            Socket socket = connect();
            if (DEBUG) { // first try
                InputStream is = socket.getInputStream();                                        
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String msg = br.readLine(); 
                System.out.println(msg);
                is.close();
            }
            if (DEBUG) { // second try
                CreateFileRequest request = new CreateFileRequest(null, null);
                ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
                try {
                    oos.writeObject(request);
                    oos.flush(); // XXX: must flush, if not packet exists in local network buffer
                    oos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            disconnect(socket);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
//        ReposSocketClient rsc = new ReposSocketClient("ReposSocketClient");
        System.exit(0);
    }

}
